﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class EditText : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string tableName = Request.QueryString["tableName"];
            string rowID = Request.QueryString["rowID"];
            string columnName = Request.QueryString["columnName"];
            if (tableName == null || rowID == null || columnName == null)
            {
                Response.SuppressContent = true;
                Response.StatusCode = 400;
                return;
            }
            AddTextBoxData(tableName, rowID, columnName);
        }
    }

    private void AddTextBoxData(string tableName, string rowID, string columnName)
    {

        try
        {
            ModdbFrontEnd frontEnd = new ModdbFrontEnd();
            DataTable dataTable = frontEnd.GetDataTable(tableName);
            Moddb08_WCF.Columns columns = frontEnd.GetAllColumns(tableName);
            Moddb08_WCF.Rows rows = frontEnd.GetAllRows(tableName);
            try
            {
                int rowIDNumber = int.Parse(rowID);
                for (int i = 0; i < rows.Count; i++)
                {
                    if (rows[i].Id == rowIDNumber)
                    {
                        for (int j = 0; j < columns.Count; j++)
                        {
                            if (columns[j].Name.Equals(columnName))
                            {


                                string test = dataTable.Rows[i][j].ToString();
                                System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
                                
                                TextBox1.Text = enc.GetString(frontEnd.GetColumnByteData(tableName, rowID, columnName));

                            }
                        }
                    }
                }
            }
            catch (FormatException)
            {
                Response.StatusCode = 400;
                Response.SuppressContent = true;
                return;
            }
        }catch(System.ServiceModel.EndpointNotFoundException)
        {
            Response.StatusCode = 400;
            Response.SuppressContent = true;
        }
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        string tableName = Request.QueryString["tableName"];
        string rowID = Request.QueryString["rowID"];
        string columnName = Request.QueryString["columnName"];
        if (tableName != null || rowID != null || columnName != null)
        {
            ModdbFrontEnd frontEnd = new ModdbFrontEnd();
            String text = TextBox1.Text;
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);
            writer.Write(text);
            stream.Position = 1;
            try
            {
                frontEnd.AddDataToColumn(stream, tableName, rowID, columnName);
            }
            catch (System.ServiceModel.EndpointNotFoundException)
            {
                Label errorText = new Label();
                errorText.Text = "Could not save the data to the database";
                Page.Controls.Add(errorText);
            }
            AddTextBoxData(tableName, rowID, columnName);
        }
        else
        {
            Response.SuppressContent = true;
            Response.StatusCode = 400;
            return;
        }
    }
}
